package by.wind.drebedengi.asynctask;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.util.Log;

public final class AsyncTaskManager implements IProgressTracker,
		OnCancelListener {

	private final OnTaskCompleteListener itsTaskCompleteListener;
	private final ProgressDialog itsProgressDialog;
	private AbstractTask itsAsyncTask;

	public AsyncTaskManager(Context context,
			OnTaskCompleteListener taskCompleteListener) {
		// Save reference to complete listener (activity)
		itsTaskCompleteListener = taskCompleteListener;
		// Setup progress dialog
		itsProgressDialog = new ProgressDialog(context);
		itsProgressDialog.setOnCancelListener(this);
	}
	
	private void setProgressStype(int aStyle) {
		if (aStyle == ProgressDialog.STYLE_HORIZONTAL) {
			itsProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
			itsProgressDialog.setIndeterminate(false);
			itsProgressDialog.setCancelable(true);
		}
		else 
		{
			itsProgressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
			itsProgressDialog.setIndeterminate(true);
			itsProgressDialog.setCancelable(false);
		}
	}

	public void setupTask(AbstractTask asyncTask) {
		// Keep task
		itsAsyncTask = asyncTask;
		setProgressStype(itsAsyncTask.getProgressType());
		// Wire task to tracker (this)
		itsAsyncTask.setProgressTracker(this);
		// Start task
		itsAsyncTask.execute();
	}

	@Override
	public void onProgress(String message, Integer... values) {
		// Show dialog if it wasn't shown yet or was removed on configuration
		// (rotation) change
		if (!itsProgressDialog.isShowing()) {
			itsProgressDialog.show();
		}
		// Show current message in progress dialog
		Log.v("AsyncTaskManager",message);
		itsProgressDialog.setMessage(message);
		if (values != null && values.length>0)
			itsProgressDialog.setProgress(values[0]);
		if (values != null && values.length>1)
			itsProgressDialog.setMax(values[1]);
	}

	@Override
	public void onCancel(DialogInterface dialog) {
		// Cancel task
		itsAsyncTask.cancel(true);
		// Notify activity about completion
		itsTaskCompleteListener.onTaskComplete(itsAsyncTask);
		// Reset task
		itsAsyncTask = null;
	}

	@Override
	public void onComplete() {
		// Close progress dialog
		itsProgressDialog.dismiss();
		// Notify activity about completion
		itsTaskCompleteListener.onTaskComplete(itsAsyncTask);
		// Reset task
		itsAsyncTask = null;
	}

	public Object retainTask() {
		// Detach task from tracker (this) before retain
		if (itsAsyncTask != null) {
			itsAsyncTask.setProgressTracker(null);
		}
		// Retain task
		return itsAsyncTask;
	}

	public void handleRetainedTask(Object instance) {
		// Restore retained task and attach it to tracker (this)
		if (instance instanceof AbstractTask) {
			itsAsyncTask = (AbstractTask) instance;
			setProgressStype(itsAsyncTask.getProgressType());
			itsAsyncTask.setProgressTracker(this);
		}
	}

	public boolean isWorking() {
		// Track current status
		return itsAsyncTask != null;
	}
}